<template>
  <div class="publishTaskList">
    <div class="topBar">
      <div class="searchBar">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          label-width="68px">
          <el-form-item label="发明名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入发明名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="专利类型" prop="patentType">
            <el-select v-model="queryParams.patentType" placeholder="请选择专利类型" clearable>
              <el-option
                v-for="dict in dict.type.patent_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="专利领域" prop="patentField">
            <el-select v-model="queryParams.patentField" placeholder="请选择专利类型" clearable>
              <el-option
                v-for="dict in dict.type.patent_field"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="发布人" prop="nikeName">
            <el-input
              v-model="queryParams.nikeName"
              placeholder="请输入发布人"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="operateBar">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:task:add']"
        >新增</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:task:export']"
        >导出</el-button>
      </div>
    </div>
    <el-table
      stripe
      height="100%"
      style="width: 100%"
      :data="taskList"
      v-loading="loading"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="下单金额" prop="amount" width="80" />
      <el-table-column
        width="300"
        label="发明名称"
        prop="name"
        show-overflow-tooltip />
      <el-table-column label="专利类型" prop="patentType" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.patent_type" :value="scope.row.patentType"/>
        </template>
      </el-table-column>
      <el-table-column
        label="专利领域"
        prop="patentField"
        width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.patent_field" :value="scope.row.patentField"/>
        </template>
      </el-table-column>
      <el-table-column
        label="要求时限"
        prop="requestTime"
        width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.requestTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="撰写要求" prop="writingRequirements" show-overflow-tooltip />
      <el-table-column label="备注" prop="remark" />
      <el-table-column label="状态" prop="state" />
      <el-table-column label="发布人" prop="userId" />
      <el-table-column label="操作" width="260">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAssign(scope.row)"
            v-hasPermi="['system:task:edit']"
          >接单任务</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="下单金额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入下单金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发明名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入发明名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="专利类型"
              prop="patentType">
              <el-select
                style="width: 100%;"
                v-model="form.patentType"
                placeholder="请选择专利类型">
                <el-option
                  v-for="dict in dict.type.patent_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="专利领域" prop="patentField">
              <el-select
                style="width: 100%;"
                v-model="form.patentField"
                placeholder="请输入专利领域">
                <el-option
                  v-for="dict in dict.type.patent_field"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="要求时限" prop="requestTime">
              <el-date-picker
                style="width: 100%;"
                clearable
                v-model="form.requestTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择要求时限">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="撰写要求" prop="writingRequirements">
              <el-input v-model="form.writingRequirements" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contacts">
              <el-input v-model="form.contacts" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人手机号" prop="contactsPhone">
              <el-input v-model="form.contactsPhone" placeholder="请输入联系人手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="state">
              <el-input v-model="form.state" placeholder="请输入状态" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布人" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入发布人" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="上传文件" prop="files">
              <el-input v-model="form.files" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 指派任务对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open1"
      width="500px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="接单人" prop="rickUserId">
          <el-input v-model="form.rickUserId"  placeholder="请输入接单人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 完成任务对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open2"
      width="500px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="接单人" prop="rickUserId">
          <el-input v-model="form.rickUserId"  placeholder="请输入接单人" />
        </el-form-item>
        <el-form-item label="上传文件" prop="files">
          <el-input v-model="form.files" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask, assignTask, completeTask, acceptTask } from "@/api/publishTaskList";

export default {
  name: "Task",
  dicts: ['patent_type', 'case_type', 'case_status', 'filing_type','patent_field'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      open1: false,
      // 是否显示弹出层
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        amount: null,
        name: null,
        patentType: null,
        patentField: null,
        requestTime: null,
        writingRequirements: null,
        contacts: null,
        contactsPhone: null,
        files: null,
        pic: null,
        state: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  async created() {
    // const result = await taskUser();
    // console.log(result)


    this.getList();
  },
  methods: {
    /** 查询任务列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;
      this.reset();
    },
    // 取消按钮
    cancel2() {
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        amount: null,
        name: null,
        patentType: null,
        patentField: null,
        requestTime: null,
        writingRequirements: null,
        contacts: null,
        contactsPhone: null,
        files: null,
        pic: null,
        remark: null,
        state: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加任务";
    },
    /** 接单任务按钮操作 */
    handleAssign(row) {
      const id = row.id;
      this.$modal.confirm('是否确认接单？').then(function() {
        return acceptTask({ id });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("接单成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 指派任务提交按钮 */
    submitForm1() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            assignTask(this.form).then(response => {
              this.$modal.msgSuccess("接单成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 完成任务提交按钮 */
    submitForm2() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            completeTask(this.form).then(response => {
              this.$modal.msgSuccess("完成成功");
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
div.publishTaskList {
  div.topBar {
    height: 36px;
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div.searchBar {
      .el-form-item {
        margin-bottom: 0;
      }
    }
  }
}
</style>

